package com.spring.demo.spring.core.applicationprocess.service;

import com.spring.demo.spring.core.annoation.ComponentFree;
import com.spring.demo.spring.core.context.BeanPostProcessor;
import org.springframework.beans.BeansException;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;


/**
 * @Author RenPu
 * @Date 2024/2/15 23:34
 * @Version 1.0
 * @Description:
 **/

@ComponentFree
public class AppBeanPostProcessor implements BeanPostProcessor {
    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {

        if("userService".equals(beanName)){

            Object newProxyInstance = Proxy.newProxyInstance(AppBeanPostProcessor.class.getClassLoader(), bean.getClass().getInterfaces(), new InvocationHandler() {
                @Override
                public Object invoke(Object o, Method method, Object[] objects) throws Throwable {

                    System.out.println("执行切面的前，需要执行的逻辑。。。。。。。");

                    return method.invoke(bean, objects);
                }
            });
            return newProxyInstance;
        }

        return bean;
    }
}
